// pages/shopping/shopping.js
const req = require('../../utils/apifm-wxapi')
Page({

  /**
   * 页面的初始数据
   */
  data: {
      shops:[],
      canIUse:true,
      isHide:false,
      userinfo:wx.getStorageSync('userinfo')||null,
      price:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //查看是否授权
    wx.getSetting({
      success: function(res) {
        if (res.authSetting['scope.userInfo']) {
          console.log("用户授权了");
        } else {
          //用户没有授权
          console.log("用户没有授权");
        }
      }
    });
    var time=setInterval(() => {
      that.setData({
      userinfo:wx.getStorageSync('userinfo')||null
    })
    },3000);
    
  },
  bindGetUserInfo: function(res) {

      if (res.detail.userInfo) {
        wx.login({
          complete: (res) => {
            console.log(res.code)
            var code =res.code
            req.login_wx(code).then(res=>{
              console.log(res)
              wx.setStorageSync('Token', res.data.token)
              wx.setStorageSync('uid', res.data.uid)
            })
          },
        })
        //用户按了允许授权按钮
  
        var that = this;
  
        // 获取到用户的信息了，打印到控制台上看下
  
        console.log("用户的信息如下：");
  
        console.log(res.detail.userInfo);
  
        //授权成功后,通过改变 isHide 的值，让实现页面显示出来，把授权页面隐藏起来
        wx.setStorageSync('userinfo', res.detail.userInfo)
        var Token= wx.getStorageSync('Token')
        req.getGoodsCart(Token).then((res)=>{
          this.setData({
            shops:res.data.items,
            price:res.data.price
          })
          console.log(this.data.shops)
        })
          if(res.code==0){
            wx.setTabBarBadge({
              index: 2,
              text: String(res.data.number),
            })
            wx.setStorageSync('number', res.data.number)
          }
        that.setData({
  
          isHide: false,
          userinfo:res.detail.userInfo
  
        });
        
      } else {
  
        //用户按了拒绝按钮
  
        wx.showModal({
  
          title: '警告',
  
          content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
  
          showCancel: false,
  
          confirmText: '返回授权',
  
          success: function(res) {
  
            // 用户没有授权成功，不需要改变 isHide 的值
  
            if (res.confirm) {
  
              console.log('用户点击了“返回授权”');
  
            }
  
          }
  
        });
  
      }
  
       
    },
    // +-number
    onChange(event) {
      console.log(event);
      var Token=wx.getStorageSync('Token')
      var number = event.detail
      var key =event.currentTarget.dataset.key
      req.goodsCartnumber(Token,number,key).then((res)=>{
        console.log(res)
        if(res.code==0){
          wx.setTabBarBadge({
            index: 2,
            text: String(res.data.number),
          })
        }
        this.setData({
          price:res.data.price
        })
      })
    },
    // 商品删除
    del(e){
      var Token=wx.getStorageSync('Token')
      var key =e.currentTarget.dataset.key
      req.goodsCartdel(Token,key).then((res)=>{
        if(res.code==0){
          this.setData({
            shops:res.data.items,
            price:res.data.price,
            number:res.data.number
          })
          wx.setTabBarBadge({
            index: 2,
            text: String(res.data.number),
          })
          wx.setStorageSync('number',res.data.number )
        }else{
          wx.removeTabBarBadge({
            index: 2,
          })
          this.setData({
            shops:{
              number:'0',
              price:'0',
              score:'0',
            }
          })
          wx.removeTabBarBadge({
            index: 2,
          })
        }
      })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var Token= wx.getStorageSync('Token')
    req.getGoodsCart(Token).then((res)=>{
      this.setData({
        shops:res.data.items,
        price:res.data.price
      })
      console.log(this.data.shops)
      if(res.code==0){
        wx.setTabBarBadge({
          index: 2,
          text: String(res.data.number),
        })
        wx.setStorageSync('number', res.data.number)
      }else{
        wx.removeTabBarBadge({
          index: 2,
        })
      }
     
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})